import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xiongan_app_yy/widgets/comm/scan_widget.dart';

//输入框Widget
class TextFieldItem extends StatelessWidget {
  final String hintText;
  final double height;
  final int mLine;
  final bool scan;
  final TextEditingController textEditingController;

  ValueChanged<String> onChangedFuns;
  ValueChanged<String> onSubmitted;
  EdgeInsets mar;
  EdgeInsets contentPad;
  bool enabled;
  List<TextInputFormatter> inputFormatters;
  TextInputType keyboardType;
  Color bgColor;
  double TextSize;
  bool isPassWord;

  TextFieldItem(
      {Key key,
      this.hintText,
      this.height,
      this.mLine = 1,
      this.textEditingController,
      this.mar,
      this.contentPad,
      this.enabled,
      this.inputFormatters,
      this.onChangedFuns,
      this.keyboardType,
      this.bgColor,
      this.TextSize,
      this.isPassWord = false,
      this.onSubmitted,
      this.scan = true})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (mar == null) {
      mar = EdgeInsets.only(top: 6, bottom: 6);
    }
    if (contentPad == null) {
      contentPad = EdgeInsets.only(left: 15, top: 11, bottom: 10);
    }
    if (inputFormatters == null) {
      inputFormatters = [];
    }
    if (keyboardType == null) {
      keyboardType = TextInputType.text;
    }
    if (bgColor == null) {
      bgColor = Colors.white;
    }
    if (TextSize == null) {
      TextSize = 16;
    }

    return Container(
      margin: mar,
      height: height,
      decoration: BoxDecoration(
          color: bgColor,
          border: Border.all(width: 0.5, color: Color(0xffcccccc)),
          borderRadius: BorderRadius.circular(6)),
      child: Center(
        child: TextField(
          inputFormatters: inputFormatters,
          keyboardType: keyboardType,
          enabled: enabled,
          obscureText: isPassWord,
          controller: textEditingController,
          maxLines: mLine,
          style: TextStyle(fontSize: 20),
          onSubmitted: (Text) {
            this.onSubmitted(Text);
          },
          onChanged: (val) {
            this.onChangedFuns(val);
          },
          decoration: InputDecoration(
              suffixIcon: scan == true
                  ? ScanWidget(
                      clickCallback: (value) {
                        if (onChangedFuns != null) {
                          onChangedFuns(value);
                        }
                        textEditingController.text = value;
                        textEditingController.selection =
                            TextSelection.fromPosition(TextPosition(
                                affinity: TextAffinity.downstream,
                                offset: textEditingController.text == null
                                    ? 0
                                    : textEditingController.text.length));
                      },
                    )
                  : null,
              hintText: hintText,
              border: InputBorder.none,
              contentPadding: contentPad,
              hintStyle: TextStyle(fontSize: TextSize)),
        ),
      ),
    );
  }
}
